package com.xkdx.guangguang.fragment.brand;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xkdx.caipiao.R;
import com.xkdx.guangguang.adapter.BrandAdapter;
import com.xkdx.guangguang.fragment.brand.search.SearchBrandFragment;
import com.xkdx.guangguang.sql.BrandDataHelper;

public class MoreBrandFragment extends Fragment {
	private View view = null;
	private Button back, brand_search, atoz;
	private ImageLoader imageLoader = ImageLoader.getInstance();
	private DisplayImageOptions options;
	private BrandDataHelper helper;
	private BrandAdapter adapter;
	private String tag;
	private int type = 0;
	private TextView title;
	private GridView gv;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		tag = getArguments().getString("tag");

		if (tag.equals("购物类") && BrandFragment.shopBrandList != null
				&& BrandFragment.shopBrandList.size() != 0) {
			adapter = new BrandAdapter(getActivity(),
					BrandFragment.shopBrandList, imageLoader);
			type = 1;
		} else if (tag.equals("美食类") && BrandFragment.foodBrandList != null
				&& BrandFragment.foodBrandList.size() != 0) {
			type = 4;
			adapter = new BrandAdapter(getActivity(),
					BrandFragment.foodBrandList, imageLoader);
		} else if (tag.equals("娱乐类")
				&& BrandFragment.entertaimentBrandList != null
				&& BrandFragment.entertaimentBrandList.size() != 0) {
			type = 2;
			adapter = new BrandAdapter(getActivity(),
					BrandFragment.entertaimentBrandList, imageLoader);
		} else if (tag.equals("生活类") && BrandFragment.lifeBrandList != null
				&& BrandFragment.lifeBrandList.size() != 0) {
			type = 3;
			adapter = new BrandAdapter(getActivity(),
					BrandFragment.lifeBrandList, imageLoader);
		}

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.more_brand, container, false);

		findView();
		setOnclick();

		return view;
	}

	private void findView() {
		back = (Button) view.findViewById(R.id.more_brand_back);
		atoz = (Button) view.findViewById(R.id.AtoZ);
		brand_search = (Button) view.findViewById(R.id.search_stroll_morebrand);
		title = (TextView) view.findViewById(R.id.more_brand_title);
		title.setText(tag);
		gv = (GridView) view.findViewById(R.id.more_brand_gv);
		gv.setAdapter(adapter);

	}

	private void setOnclick() {
		brand_search.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				SearchBrandFragment searchBrandFragment = new SearchBrandFragment();
				getFragmentManager().beginTransaction()
						.replace(R.id.fragment, searchBrandFragment)
						.addToBackStack(null).commit();
			}
		});

		atoz.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				AtoZFragment atoZFragment = new AtoZFragment();
				Bundle b = new Bundle();
				b.putString("tag", type + "");
				atoZFragment.setArguments(b);
				getFragmentManager().beginTransaction()
						.replace(R.id.fragment, atoZFragment)
						.addToBackStack(null).commit();
			}
		});

		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				FragmentManager fm = getFragmentManager();
				fm.popBackStack();
			}
		});

		gv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				switch (type) {
				case 1:
					FragmentTransaction ft = getFragmentManager()
							.beginTransaction();
					BrandInfoFragment brandShopFragment = new BrandInfoFragment();
					Bundle b = new Bundle();
					b.putString("brandID",
							String.valueOf(BrandFragment.shopBrandList.get(position)
									.getBrandID()));
					brandShopFragment.setArguments(b);
					ft.replace(R.id.fragment, brandShopFragment);
					ft.addToBackStack(null);
					ft.commit();
					break;
				case 2:
					FragmentTransaction ft2 = getFragmentManager()
							.beginTransaction();
					BrandInfoFragment brandShopFragment2 = new BrandInfoFragment();
					Bundle b2 = new Bundle();
					b2.putString("brandID", String.valueOf(BrandFragment.entertaimentBrandList
							.get(position).getBrandID()));
					brandShopFragment2.setArguments(b2);
					ft2.replace(R.id.fragment, brandShopFragment2);
					ft2.addToBackStack(null);
					ft2.commit();
					break;
				case 3:
					FragmentTransaction ft3 = getFragmentManager()
							.beginTransaction();
					BrandInfoFragment brandShopFragment3 = new BrandInfoFragment();
					Bundle b3 = new Bundle();
					b3.putString("brandID",
							String.valueOf(BrandFragment.lifeBrandList.get(position)
									.getBrandID()));
					brandShopFragment3.setArguments(b3);
					ft3.replace(R.id.fragment, brandShopFragment3);
					ft3.addToBackStack(null);
					ft3.commit();
					break;
				case 4:
					FragmentTransaction ft4 = getFragmentManager()
							.beginTransaction();
					BrandInfoFragment brandShopFragment4 = new BrandInfoFragment();
					Bundle b4 = new Bundle();
					b4.putString("brandID",
							String.valueOf(BrandFragment.foodBrandList.get(position)
									.getBrandID()));
					brandShopFragment4.setArguments(b4);
					ft4.replace(R.id.fragment, brandShopFragment4);
					ft4.addToBackStack(null);
					ft4.commit();
					break;
				default:
					break;
				}

			}
		});

	}

}
